package com.rz.paas.test.inChannel.adapter

import android.content.Context
import android.util.Log
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.core.view.get
import com.rz.paas.baselib.recyclerView.CommonAdapter
import com.rz.paas.baselib.recyclerView.CommonHolder
import com.rz.paas.test.R


class VideoListAdapter(val context: Context) :
    CommonAdapter<VideoData>(context, R.layout.video_list_item),
    Observable {
    override fun bindData(holder: CommonHolder, data: VideoData, position: Int) {
        val videoViewContainer = holder.findView<FrameLayout>(R.id.video_view_container)
        if (data.isTopping) {
            videoViewContainer.removeAllViews()
        } else {
            if (videoViewContainer.childCount <= 0) {
                val imageView = ImageView(context)
                val layoutParams = FrameLayout.LayoutParams(
                    FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT
                )
                imageView.layoutParams = layoutParams
                videoViewContainer.addView(imageView)
            }
            val view = videoViewContainer[0]
            (view as ImageView).setImageResource(data.imageRes)
        }

        var findView = holder.findView<FrameLayout>(R.id.close_camera_tips)
        if (data.closeCamera) {
            findView.visibility = View.VISIBLE
        } else {
            findView.visibility = View.INVISIBLE
        }


        if (data.isTopping) {
            holder.findView<ImageView>(R.id.cover_image).visibility = View.VISIBLE
        } else {
            holder.findView<ImageView>(R.id.cover_image).visibility = View.INVISIBLE
        }

        iconTips(data, holder, position)
    }

    private fun iconTips(videoData: VideoData, holder: CommonHolder, position: Int) {

        holder.findView<LinearLayout>(R.id.icon_container).removeAllViews()
        removeViewByParent(videoData.switch_camera_iv!!)
        removeViewByParent(videoData.topping_operator_iv!!)
        removeViewByParent(videoData.camera_operator_iv!!)
        removeViewByParent(videoData.mic_operator_iv!!)

        addView(holder, getCameraIcon(videoData.camera_operator_iv!!, videoData))
        addView(holder, getMicIcon(videoData.mic_operator_iv!!, videoData))

        if (videoData.isSelf) {
            addView(holder, videoData.switch_camera_iv as View)
            addView(holder, videoData.topping_operator_iv as View)
            removeView(holder, videoData.hd_operator_iv as View)
        } else {
            removeView(holder, videoData.switch_camera_iv as View)
            removeView(holder, videoData.topping_operator_iv as View)
            addView(holder, videoData.hd_operator_iv as View)
        }


        holder.itemView.setOnClickListener {
            Log.d(TAG, "itemPosition:position:$position selectedPosition:$selectedPosition")
            changeView(position, holder.itemView)
            videoData.isTopping = true
            if (selectedPosition != -1 && position != selectedPosition) {
                data[selectedPosition].isTopping = false
            }
            selectedPosition = position
        }
        val findView = holder.findView<LinearLayout>(R.id.icon_container)
        if (findView.childCount > 0) {
            //camera operator
            findView[0].setOnClickListener {
                videoData.muteCamera = !videoData.muteCamera
                getCameraIcon(videoData.camera_operator_iv!!, videoData)
                findSameUserIdData(position, videoData)
            }
        }

        if (findView.childCount > 1) {
            //microphone operator
            findView[1].setOnClickListener {
                videoData.muteMicrophone = !videoData.muteMicrophone
                getMicIcon(videoData.mic_operator_iv!!, videoData)
                findSameUserIdData(position, videoData)
            }
        }


    }


    fun findSameUserIdData(position: Int, videoData: VideoData) {
        for (index in 0 until data.size) {
            if (data[index].userId == videoData.userId && position != index) {
                data[index].muteCamera = videoData.muteCamera
                data[index].muteMicrophone = videoData.muteMicrophone
                getCameraIcon(data[index].camera_operator_iv!!, data[index])
                getMicIcon(data[index].mic_operator_iv!!, data[index])
                break
            }
        }
    }

    fun getCameraIcon(iv: ImageView, videoData: VideoData): View {
        iv.alpha = 1f
        if (videoData.closeCamera && videoData.muteCamera) {
            iv.setImageResource(R.mipmap.icon_close_camera)
            iv.alpha = 0.5f
        } else if (videoData.closeCamera) {
            iv.setImageResource(R.mipmap.icon_close_camera)
        } else if (videoData.muteCamera) {
            iv.setImageResource(R.mipmap.icon_mute_camera)
        } else {
            iv.setImageResource(R.mipmap.camera_close_icon)
        }
        return iv
    }

    fun getMicIcon(iv: ImageView, videoData: VideoData): View {
        iv.alpha = 1f
        if (videoData.closeMicrophone && videoData.muteMicrophone) {
            iv.setImageResource(R.mipmap.icon_close_microphone)
            iv.alpha = 0.5f
        } else if (videoData.closeMicrophone) {
            iv.setImageResource(R.mipmap.icon_close_microphone)
        } else if (videoData.muteMicrophone) {
            iv.setImageResource(R.mipmap.icon_mute_microphone)
        } else {
            iv.setImageResource(R.mipmap.mic_open_icon)
        }
        return iv
    }

    var selectedPosition = -1
    private fun removeViewByParent(view: View) {
        if (view.parent != null)
            (view.parent as LinearLayout).removeView(view)
    }

    private fun removeView(holder: CommonHolder, childView: View) {
        holder.removeView(R.id.icon_container, childView)
    }

    private val TAG = "VideoListAdapter"
    private fun addView(holder: CommonHolder, childView: View) {
        val miclayoutParams = LinearLayout.LayoutParams(dp2px(20), dp2px(20))
        miclayoutParams.gravity = Gravity.LEFT
        miclayoutParams.setMargins(0, 5, 5, 0)
        childView.layoutParams = miclayoutParams
        holder.addView(R.id.icon_container, childView)
    }

    private fun dp2px(dp: Int): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            dp * 1.0f,
            context.resources.displayMetrics
        ).toInt()
    }

    val observers = mutableListOf<Observer>()

    private fun changeView(position: Int, itemView: View) {
        for (observer in observers) {
            observer.update(position, itemView)
        }
    }


    fun isItemHeader(position: Int): Boolean {
        if (position == 0) {
            return true
        }
        return false
    }

    override fun addObserver(o: Observer) {
        observers.add(o)
    }

    override fun deleteObservers() {
        observers.clear()

    }
}